package io;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.Batoh;
import model.VecDoBatohu;

public class Input {

	 private String nazovVstupu;

	    public Input(String vstupnySubor) {
	        nazovVstupu = vstupnySubor;
	    }

	     public List<Batoh> nacitajVstupnySuborSInstanciou() {
	        List<Batoh> zoznamInstancii = new ArrayList<Batoh>();
	        FileInputStream fstream = null;
	        DataInputStream in = null;
	        BufferedReader br = null;
	        String nacitaneData;


	        double cenaVeci = 0.0;
	        double vahaVeci = 0.0;
	        int cisloInstancie = 0;
	        int pocetVeciVBatohu = 0;
	        int maxVaha = 0;
	        try {
	        	System.out.println("NAZOV VSTUPU je" + nazovVstupu);
	            fstream = new FileInputStream(new File(nazovVstupu));
	            in = new DataInputStream(fstream);
	            br = new BufferedReader(new InputStreamReader(in));
	            while ((nacitaneData = br.readLine()) != (null)) {
	                Batoh aktualnaInstancia = null;
	                Scanner sc = new Scanner(nacitaneData).useDelimiter(" ");
	                cisloInstancie = sc.nextInt();
	                pocetVeciVBatohu = sc.nextInt();
	                //System.out.println("Veci je " +pocetVeciVBatohu);
	                maxVaha = sc.nextInt();
	                aktualnaInstancia = new Batoh(maxVaha, pocetVeciVBatohu, cisloInstancie);
	                for (int i = 0; i < pocetVeciVBatohu; i++) {
	                    vahaVeci = sc.nextDouble();
	                    cenaVeci = sc.nextDouble();
	                    VecDoBatohu vkladanaVec = new VecDoBatohu(cenaVeci, vahaVeci, ((double) cenaVeci) / ((double) vahaVeci), i);
	                    aktualnaInstancia.pridajVecDoBatohu(vkladanaVec);
	                } //vsetky veci su uz priradene do batohu, instancia je nacitana
	                zoznamInstancii.add(aktualnaInstancia);
	            //System.out.println("Batoh " + aktualnaInstancia.maxHmotnost + " " + aktualnaInstancia.pocetVeciVbatohu + "  bol prave vytvoreny");
	            }
	            return zoznamInstancii;
	        } catch (FileNotFoundException ex) {
	            System.out.println("Subor zadany ako argument nebol najdeny");
	            Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, ex);
	            return null;
	        } catch (IOException ex) {
	            Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, ex);
	            return null;
	        } catch (Exception ex) {
	            System.out.println("Some exception");
	            return null;
	        } finally {
	            try {
	                fstream.close();
	                in.close();
	                br.close();

	            } catch (IOException ex) {
	                Logger.getLogger(Input.class.getName()).log(Level.SEVERE, null, ex);
	                return null;
	            }
	        }

	    }

		public String getNazovVstupu() {
			return nazovVstupu;
		}

		public void setNazovVstupu(String nazovVstupu) {
			this.nazovVstupu = nazovVstupu;
		}


}
